package com.zxjbyte.yiyi.module.system.controller.devconfig;

import com.zxjbyte.yiyi.framework.common.domain.api.PageResult;
import com.zxjbyte.yiyi.framework.common.domain.api.R;
import com.zxjbyte.yiyi.module.system.domain.param.DevConfigAddParam;
import com.zxjbyte.yiyi.module.system.domain.param.DevConfigListParam;
import com.zxjbyte.yiyi.module.system.domain.param.DevConfigUpdateParam;
import com.zxjbyte.yiyi.module.system.domain.vo.DevConfigVO;
import com.zxjbyte.yiyi.module.system.manager.DevConfigManager;
import com.zxjbyte.yiyi.module.system.service.DevConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Author zhangxingjia
 * @Date 2024/9/12 13:50
 * @Version: 1.0
 */
@Slf4j
@Tag(name = "配置接口")
@RestController
@RequestMapping(value = "/system/devconfig")
public class DevConfigController {

    @Resource
    private DevConfigService devConfigService;

    @Operation(summary = "系统配置分页查询")
    @GetMapping("/list")
    public PageResult<DevConfigVO> list(@ParameterObject DevConfigListParam param){
        return devConfigService.list(param);
    }

    @Operation(summary = "系统配置详情")
    @GetMapping("/detail")
    public R<DevConfigVO> detail(@Parameter(description = "唯一id" ) @RequestParam Long id){
        return R.data(devConfigService.detail(id));
    }

    @Operation(summary = "新增系统配置")
    @PostMapping("/add")
    public R<?> addDevConfig(@Valid @RequestBody DevConfigAddParam param){
        devConfigService.addDevConfig(param);
        return R.ok();
    }

    @Operation(summary = "更新系统配置")
    @PutMapping("/update")
    public R<?> updateDevConfig(@Valid @RequestBody DevConfigUpdateParam param){
        devConfigService.updateDevConfig(param);
        return R.ok();
    }

    @Operation(summary = "删除系统配置")
    @DeleteMapping("/delete")
    public R<?> deleteDevConfig(@RequestParam(name = "id") Long id){
        devConfigService.deleteDevConfig(id);
        return R.ok();
    }

    @Operation(summary = "重新加载系统配置")
    @GetMapping("/reload")
    public R<?> reload(@Parameter(description = "配置键" ) @RequestParam(required = false) String configKey){
        DevConfigManager.getInstance().reload(configKey);
        return R.ok();
    }
}
